from openai import OpenAI

# 文件解析密钥
def KeyWord(path,keyword):
    with open(path,'r',encoding="utf-8") as f:
        context = f.readlines()
        for item in context:
            if keyword in item:
                return item.split(" ")[-1].replace('"',"").strip()
        else:
            raise Exception("Keyword not found...")


# deepseek
def Bot_Deepseek(path):
    # 调用参数
    DEEPSEEK_API = KeyWord(path,"deepseek-api")
    DEEPSEEK_URL = KeyWord(path,"deepseek-url")

    # 调用AI
    ## 初始化
    client = OpenAI(api_key=DEEPSEEK_API,base_url=DEEPSEEK_URL)
    messages = []
    print("欢迎使用DeepSeek")
    while True:
        question = input("\n用户：")
        if question.upper() == "QUIT":
            print("期待您的下次使用，再见!")
            break

    # 调用大语言模型
        messages.append({"role":"user","content":question})
        response = client.chat.completions.create(
            model="deepseek-chat",
            messages=messages,
            stream=False
        )

        # 大语言模型回复
        reply = response.choices[0].message.content
        print(11)
        print(f"\nDeepSeek: {reply}")

        messages.append({"role":"assistant","content":reply})


path = "./bot.txt"
Bot_Deepseek(path)

